<template>
  <el-dialog title="关联进项发票明细" :visible.sync="dialogVisible" width="1400px" :close-on-click-modal="false"
    :modal-append-to-body="false" @close="dialogVisible=false" v-drag2anywhere>
    <div>
      <div class="title">销项明细</div>
      <el-table border :data="salelist">
        <el-table-column label="序号" type="index" width="50px" align="center"></el-table-column>
        <el-table-column label="开票日期" prop="date" width="140px" align="center"></el-table-column>
        <el-table-column label="购方名称" prop="buyer" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column label="销项票号" prop="fphm" width="165px" align="center"></el-table-column>
        <el-table-column label="类型" prop="type" width="110px" align="center"></el-table-column>
        <el-table-column label="税收类目" align="left" width="220px">
          <template v-slot="scope">
            {{scope.row.taxname}} ({{scope.row.title}})
          </template>
        </el-table-column>
        <el-table-column label="规格" prop="model" align="center"></el-table-column>
        <el-table-column label="单位" prop="unit" width="60px" align="center"></el-table-column>
        <el-table-column label="数量" prop="count" width="60px" align="center"></el-table-column>
        <el-table-column label="金额" prop="total" width="100px" align="center"></el-table-column>
        <el-table-column label="税额" prop="tax" width="95px" align="center"></el-table-column>
        <el-table-column label="合计" prop="sum" width="95px" align="center"></el-table-column>
        <el-table-column label="销方名称" prop="seller" align="center" show-overflow-tooltip></el-table-column>

        <el-table-column label="发票代码" prop="code" v-if="false"></el-table-column>
      </el-table>
    </div>

    <!--对应进项-->
    <div style="margin-top: 20px;">
      <div class="title">对应进项票</div>
      <div>
        <div style="text-align: right; padding: 2px 2px 5px 0px;">
          <el-button type="primary" size="mini" icon="el-icon-finished" @click="buyFilterUIShow">添加进项票</el-button>
        </div>
        <div>
          <!--绑定进项明细-->
          <el-table border :data="buylist">
            <el-table-column label="序号" type="index"></el-table-column>
            <el-table-column label="开票日期" prop="buyticketdate" width="140px"></el-table-column>
            <el-table-column label="开票方名称" prop="seller"  align="center" show-overflow-tooltip></el-table-column>
            <el-table-column label="类型" prop="type" width="150px" align="center">
              <template v-slot="scope">
                {{scope.row.type}}({{scope.row.rate*100}}%)
              </template>
            </el-table-column>
            <el-table-column label="进项发票号" prop="buyticketfphm" width="150px" align="center"></el-table-column>
            <el-table-column label="税收类目及名称" align="left" width="200px">
              <template v-slot="scope">
               * {{scope.row.taxname}}*{{scope.row.title}}
              </template>
            </el-table-column>
            <el-table-column label="规格型号" prop="model"></el-table-column>
            <el-table-column label="单位" prop="unit" width="80px" align="center"></el-table-column>
            <el-table-column label="数量" prop="count" width="80px" align="center"></el-table-column>
            <el-table-column label="金额" prop="total" width="95px" align="center"></el-table-column>
            <el-table-column label="备注" prop="memo"></el-table-column>
            <el-table-column label="购方名称" prop="buyer" show-overflow-tooltip>
              <template v-slot="scope">
                <span v-if="scope.row.buyer!=formData.seller" style="color: #FF0000;">{{scope.row.buyer}}</span>
                <span v-if="scope.row.buyer==formData.seller" >{{scope.row.buyer}}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="50px">
              <template v-slot="scope">
                <el-button-group>
                  <el-button type="danger" icon="el-icon-delete" circle size="mini"
                    @click="handleDelete(scope.$index,scope.row)"></el-button>
                </el-button-group>
              </template>
            </el-table-column>
          </el-table>
          <div style="text-align: right; line-height: 35px;">
            <span style="margin-right: 20px;">数量:{{parseFloat(sumcount).toFixed(3)}}</span>
            <span>金额:{{parseFloat(sumtotal).toFixed(3)}}</span>
          </div>
        </div>
      </div>
    </div>
    <div style="margin-top: 20px;">
      <div class="title">关联状态</div>
      <div>
        <el-form :model="formData" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="关联状态">
                <el-select v-model="formData.buyticket_bindstatus">
                  <el-option label="不完整" value="0"></el-option>
                  <el-option label="完整" value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="关联备注">
                <el-input type="textarea" v-model="formData.buyticket_bindstatusmemo" :rows="5"
                  placeholder="请在此处填写关联相关备注信息"></el-input>
              </el-form-item>
            </el-col>

          </el-row>
        </el-form>
      </div>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false" size="medium">取 消</el-button>
      <el-button type="primary" size="medium" @click="handleSave">确定</el-button>
    </div>

    <BuyDetailFilterUI v-if="buyFilterVisible" ref="buyFilterUI" @completeHandler="buyFilterUICallback">
    </BuyDetailFilterUI>

  </el-dialog>
</template>

<script>
  import BuyDetailFilterUI from './buyticketfilter.vue';
  export default {
    components: {
      BuyDetailFilterUI
    },
    data() {
      return {
        index: -1,
        formData: {
          id: "",
          bind_buystatus: 0,
          bind_buystatusmemo: ''
        },
        buylist: [],
        salelist: [],
        dialogVisible: false,
        buyFilterVisible: false,
        sumcount:0,
        sumtotal:0,
      }
    },
    methods: {
      buyFilterUIShow() {
        this.buyFilterVisible = true;
        this.$nextTick(() => {
          this.$refs.buyFilterUI.dialogVisible = true;
          this.$refs.buyFilterUI.initData(this.formData.seller);
        })
      },
      buyFilterUICallback(list) {
        //处理数据
         console.log("绑定",list);
        for (var i = 0; i < list.length; i++) {
          this.buylist.push({
            id: -1,
            saleticketdid: this.formData.id,
            buyticketid:list[i].buyticketid,
            buyticketdid: list[i].buyticketdid,
            buyticketfphm:list[i].buyticketfphm,
            saleticketid:this.formData.saleticketid,
            saleticketfphm:list[i].fphm,
            buyticketdate:list[i].date,
            buyticketfphm: list[i].num,
            taxname: list[i].taxname,
            taxcode: list[i].taxcode,
            title: list[i].title,
            seller:list[i].seller,
            buyer:list[i].buyer,
            model: list[i].model,
            unit: list[i].unit,
            type: list[i].type,
            rate: list[i].rate,
            count: list[i].readycount,
            total: list[i].dsum,
            memo: list[i].dmemo
          });

        }
        this.handleRefreshBottom();

      },
      initData(index, data) {
        this.index = index;
        this.formData = data;
        this.salelist = [data];
        this.loadSaleBuyDetailList();
      },
      handleRefreshBottom(){
        this.sumcount=0;
        this.sumtotal=0;
         this.buylist.forEach(item=>{
             this.sumcount+=parseFloat(item.count);
             this.sumtotal+=parseFloat(item.total);
         })
         if(this.buylist.length==0){
           this.formData.num="";
         }
      },
      loadSaleBuyDetailList() {
        var params = {
          stdid: this.formData.id
        };
        this.axios.post("api/admin/saleticketdetail/getbindbuyticketlist", params).then(res => {
          if (res.status == 200 && res.data.code) {
            this.buylist = res.data.list;
            if(this.buylist.length==0){
              this.formData.num="";
            }
            this.handleRefreshBottom();
          }
        });
      },
      handleDelete(index, data) {
        this.$confirm("确定要删除此项吗?", "提示").then(() => {
            this.buylist.splice(index, 1);
            this.handleRefreshBottom();
        });

      },
      handleSearch() {
        this.curpage = 1;
        this.loadDataList();
      },
      handleSave() {
        if (this.sumcount==0 && this.formData.buyticket_bindstatus == 1) {
          this.$confirm("当前未关联任何票，但进项状态为【完整】,是否继续提交?", "信息确认", {
            type: 'warning'
          }).then(() => {
            this.finalSave();
          }).catch((err) => {
            return;
          })
        }else if(this.sumcount<this.formData.count && this.formData.buyticket_bindstatus==1){
          this.$confirm("当前已关联进项数量与销项不匹配，但进项状态为【完整】,是否继续提交?", "信息确认", {
            type: 'warning'
          }).then(() => {
            this.finalSave();
          }).catch((err) => {
            return;
          })

        }else if (this.sumcount>=this.formData.count && this.formData.buyticket_bindstatus == 0) {
          this.$confirm("当前已关联进项数量大于销项数量，但关联状态为【不完整】,是否继续提交?", "信息确认", {
            type: 'warning'
          }).then(() => {
            this.finalSave();
          }).catch((err) => {
            return;
          })
        }else{
          this.finalSave();
        }
      },
      finalSave() {
          const params = {
          saledid: this.formData.id,
          saleid: this.formData.stid,
          salenum: this.formData.fphm,
          status: this.formData.buyticket_bindstatus,
          memo: this.formData.buyticket_bindstatusmemo,
          bindlist: this.buylist
        }

        this.axios.post("api/admin/saleticketdetail/update_buyticket_bindlist", params).then(res => {
          if (res.status == 200 && res.data.code) {
            this.$message.success({
              message: res.data.msg,
              duration: 1500,
              showClosed: true,
            });
            console.log(res.data);
            this.formData.buyticket_bindnum=res.data.num;
            this.formData.buyticket_bindstatus=res.data.status;
            this.dialogVisible = false;
            this.$emit("updated", this.index, this.formData);
          } else {
            this.$message.error({
              message: res.data.msg,
              duration: 1500,
              showClosed: true
            });
          }
        });


        this.$emit("completeHandler", this.selection);
        //this.dialogVisible=false;
      },
      handleSizeChange: function(val) {
        this.pagesize = val;
        this.curpage = 1;
        this.loadDataList();
      },
      handleCurrentChange: function(val) {
        this.curpage = parseInt(val);
        this.loadDataList();
      },
    }
  }
</script>

<style scoped>
  .title {
    position: relative;
    padding-left: 10px;
    margin-bottom: 15px;
    font-size: 14px;
    color: #333;
    font-weight: 700;
  }

  .title:before {
    content: "";
    position: absolute;
    top: 4px;
    left: 0;
    width: 5px;
    height: 65%;
    background-color: #249af3;
  }
</style>
